package edu.action;



import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.model.Patient;
import edu.model.User;
import edu.model.UserRole;
import edu.service.AuthorityService;
import edu.service.PatientService;


public class LoginAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2116833997089520571L;
	
	
	private String username = null;
	private String password = null;
	private List<Patient> patientList = null;
	//private User user = null;
	private AuthorityService authorityService = null;
	private PatientService patientService = null;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public List<Patient> getPatientList() {
		return patientList;
	}

	public void setPatientList(List<Patient> patientList) {
		this.patientList = patientList;
	}

	public PatientService getPatientService() {
		return patientService;
	}

	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}

	public AuthorityService getAuthorityService() {
		return authorityService;
	}

	public void setAuthorityService(AuthorityService authorityService) {
		this.authorityService = authorityService;
	}
	@SuppressWarnings("unchecked")
	public String login(){
		
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(password.getBytes());
			password = new BigInteger(1, md.digest()).toString(16);
		
			User user = authorityService.getUser(username, password);
			if(user != null){
				
				Map session = ActionContext.getContext().getSession();
				session.put("loginUser", user);
				session.put("loginRole", ((UserRole)user.getUserRoles().toArray()[0]).getRole());
				System.out.println("login " + session.get("loginUser"));
				return SUCCESS;
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ERROR;
	}
	@SuppressWarnings("unchecked")
	public String logout(){
		System.out.println("logout action");
		
		Map session = ActionContext.getContext().getSession();
		User user = (User)session.get("loginUser");
		if(user != null) {
			session.remove("loginUser");
			session.remove("loginRole");
		}
		HttpServletRequest request = ServletActionContext.getRequest ();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setHeader("Cache-Control", "no-cache");
	    response.setHeader("Pragma", "no-cache");
	    response.setDateHeader("Expires", -1);
		request.getSession().invalidate();
		return SUCCESS;
	}
}
